// pages/jiaocinfo/jiaocinfo.js
const API = require("../../http/API")
const http = require("../../http/http")
Page({
  data: {
    // 保存数据
    course: '',
    // 参数页数
    pagenum: 1,
    keyword: ''
  },
  // 触底加载判断
  onReachBottom: function () {
    if (!this.data.keyword) {
      http(API.search, {
        page: this.data.pagenum,
        software: 0,
        free: "Y"
      }).then(res => {
        this.setData({
          course: [...this.data.course, ...res.data.data],
          pagenum: this.data.pagenum + 1
        })
      })
    } else {
      this.setData({
        pagenum: this.data.pagenum + 1
      })
      http(API.search, {
        page: this.data.pagenum,
        software: 0,
        keyword: this.data.keyword
      }).then(res => {
        this.setData({
          course: [...this.data.course, ...res.data.data],
        })
        if (res.data.data == false) {
          wx.showToast({
            title: '没有更多的数据了',
            icon: 'none'
          })
        }
      })
    }
  },
  // 搜索
  keywordsChange(e) {
    this.setData({
      pagenum: 1,
      keyword: e.detail.value
    })
    http(API.search, {
      page: this.data.pagenum,
      software: 0,
      keyword: this.data.keyword
    }).then(res => {
      this.setData({
        course: res.data.data,
      })
    })
  },
  // 免费教程详细接口
  onLoad(options) {
    // 免费教程详细接口
    http(API.search, {
      page: this.data.pagenum,
      software: 0,
      free: "Y"
    }).then(res => {
      console.log(res);
      this.setData({
        course: res.data.data,
        pagenum: this.data.pagenum + 1
      })
    })
  },
})